<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\controller\Base;
use app\common\model\Infinite;

use \think\Request;

require_once '../extend/Alipay/aop/AopClient.php';

class PayNotify extends Base
{

    /**
     * 微信小程序支付回调
     */
    public function wx_notify()
    {
        $xmldata = file_get_contents("php://input");
        libxml_disable_entity_loader(true);

        //处理微信支付返回的xml数据
        $jsonxml = json_encode(simplexml_load_string($xmldata, 'simplexmlelement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);//转成数组，

        $sign_return = $result['sign'];
        $sign = $this->appgetsign($result);
        if ($sign == $sign_return && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            //把数据提交给订单处理方法
            $out_trade_no = $result['out_trade_no'];
            $attach = $result['attach'];
            writelog($out_trade_no, "支付成功--");
            $data = $this->dispenses($attach, $out_trade_no);
            if ($data == 1) {
                return $this->returnxml();
            }
        }
    }

    /**
     * App微信支付回调
     */
    public function wx_app_notify()
    {
        $xmldata = file_get_contents("php://input");
        libxml_disable_entity_loader(true);

        //处理微信支付返回的xml数据
        $jsonxml = json_encode(simplexml_load_string($xmldata, 'simplexmlelement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);//转成数组，

        // writelog('11', json_decode($result,1));
        $sign_return = $result['sign'];
        $sign = $this->wx_appgetsign($result);
        if ($sign == $sign_return && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            //把数据提交给订单处理方法
            $out_trade_no = $result['out_trade_no'];
            $attach = $result['attach'];
            writelog($out_trade_no, "支付成功--");
            $data = $this->dispenses($attach, $out_trade_no);
            if ($data == 1) {
                return $this->returnxml();
            }
        }
    }


    //App 支付宝支付回调
    public function ali_notify()
    {
        $config = getConfig("alipay");
        $aop = new \AopClient;
        $aop->alipayrsaPublicKey = $config['aliPublicKey'];
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        writelog($_POST['out_trade_no'], json_encode($_POST));
        if ($flag) {
            if ($_POST['trade_status'] != 'TRADE_SUCCESS') {
                writelog($_POST['out_trade_no'], '【订单状态】：' . $_POST['trade_status']);
                return 'success';
            }
            $attach = $_POST['passback_params'];
            $out_trade_no = $_POST['out_trade_no'];
            writelog($out_trade_no, "支付成功--");
            $data = $this->dispenses($attach, $out_trade_no);
            //  $data = $this->dispenses_cp($attach, $out_trade_no);
            if ($data == 1) {
                return 'success';
            }
        } else {
            writelog($_POST['out_trade_no'], '验证失败|');
            return false;
        }
    }

    /**
     * @param string $attach 类型
     * @param string $out_trade_no 订单号
     */
    protected function dispenses($attach = '', $out_trade_no = '')
    {
        if ($attach == 'product_notify') {
            //商城回调
            $notify = new \app\api\controller\Notify($this->app);
            $data = $notify->order_update($out_trade_no, 2);
        }
        return $data;
    }

    /**
     * 格式化参数格式化成url参数  生成签名sign
     */
    public function appgetsign($data)
    {
        $weixinpay = getConfig('wxpay');
        $appwxpay_key = $weixinpay['mch_key'];
        //签名步骤一：按字典序排序参数
        ksort($data);
        $string = $this->callbacktourlparams($data);
        //签名步骤二：在string后加入key
        if ($appwxpay_key) {
            $string = $string . "&key=" . $appwxpay_key;
        }
        //签名步骤三：md5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result_ = strtoupper($string);
        return $result_;
    }

    public function wx_appgetsign($data)
    {
        $weixinpay = getConfig('wxpay_app');
        $appwxpay_key = $weixinpay['mch_key'];
        //签名步骤一：按字典序排序参数
        ksort($data);
        $string = $this->callbacktourlparams($data);
        //签名步骤二：在string后加入key
        if ($appwxpay_key) {
            $string = $string . "&key=" . $appwxpay_key;
        }
        //签名步骤三：md5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result_ = strtoupper($string);
        return $result_;
    }

    /**
     * 格式化参数格式化成url参数
     */
    public function callbacktourlparams($parameters)
    {
        $buff = "";
        foreach ($parameters as $k => $v) {
            if ($k != "sign" && $v != "" && !is_array($v)) {
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }

    /**
     * @param 拼装xml数据返回
     * @author  yangzl <[<email address>]>
     */
    public function returnxml()
    {
        header("content-type:text/xml;");
        $xml = "<?xml version='1.0' encoding='utf-8'?>\n";
        $xml .= "<xml>\n";
        $xml .= "<return_code>success</return_code>\n";
        $xml .= "<return_msg>ok</return_msg>\n";
        $xml .= "</xml>\n";
        echo $xml;
    }
}